import { combineReducers } from 'redux'
import { configureStore } from '@reduxjs/toolkit'
import settingSlice from './slices/setting.slice'
import globalSlice from './slices/global.slice'
import { persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'

export type StoreStateType = 'setting' | 'global'

export type StoreState<T> = {
    [key in StoreStateType]: T
}

// 持久化配置
const persistConfig = {
    key: 'root', // 存储的键名
    storage, // 保存在localStorage中
    whitelist: ['setting'], // 持久化白名单
}

// 合并reducer
const reducers = combineReducers({
    setting: settingSlice.reducer,
    global: globalSlice.reducer,
})

const persistedReducer = persistReducer(persistConfig, reducers)

const store = configureStore({
    reducer: persistedReducer,
    middleware: (getDefaultMiddleware) =>
        getDefaultMiddleware({
            serializableCheck: false, // 解决序列化问题
        }),
})

export default store
